<?php
namespace app\controller;

use app\service\R2Service;
use think\Db;
class Cli extends Base
{
    function test() {
        $r2Service = new R2Service();
        $url = $r2Service->uploadFile('res/2025/05/23/3988919090.jpg','2025/05/23/3988919090.jpg');
        debug_dump($url);
    }

    function repair_title(){
        $wallpapers = Db::name('wallpaper')->where('status',1)->select();
        foreach($wallpapers as $row){
            $title = $row['title'];
            $title = removeAllBracketsContent($title);
            if ($title == $row['title']){
                continue;
            }
            Db::name('wallpaper')->where('id',$row['id'])->update(['title' => $title]);
            echo "repair {$row['title']} to {$title} id {$row['id']} \n";
        }
    }
    public function repair_noindex(){
        $wallpapers = Db::name('wallpaper')->group('title')->select();
        foreach($wallpapers as $row){
            $title = $row['title'];
            $wallpaper_title_datas = Db::name('wallpaper')->where('title',$title)->where('status',1)->select();
            foreach($wallpaper_title_datas as $wallpaper_title_data){
                if ($wallpaper_title_data['noindex'] == 0){
                    Db::name('wallpaper')->where('title',$title)->update(['noindex' => 0]);
                    $ids = implode(',',array_column($wallpaper_title_datas,'id'));
                    echo "repair title {$title} ids {$ids} \n";
                    break;
                }

            }
        }
    }

    function r2_upload() {
        $r2Service = new R2Service();
        $datas = db('wallpaper')->order('id desc')->where(['is_r2' => 0])->limit(50000)->select();
        foreach($datas as $row){
            $file_path = json_decode($row['file_path'],true);
            $new_file_path = $file_path;
            foreach($file_path as $key => $file){
                $dest_file = 'res'. $file;
                if (empty($file) || !is_file($dest_file)){
                    continue;
                }
                $bucket_key = substr($file,1);
                $url = $r2Service->uploadFile($dest_file,$bucket_key);
                // debug_dump($url);
                if($url){
                    $new_file_path[$key] = $url;
                }
            }
            $new_file_path = json_encode($new_file_path);
            db('wallpaper')->where(['id' => $row['id']])->update(['file_path' => $new_file_path,'is_r2' => 1]);
            echo $row['id']." \n";
            // exit;
            
        }
    }
    function remove_noindex_by_keyword($kw) {
        $kw = trim($kw);
        $cache_category_key = 'last_update_time_category_id_';
        $cache_top_category_key = 'last_update_time_top_category_';
        $datas = db('wallpaper')->where(['noindex' => 1])->where('title','like','%'.$kw.'%')->select();
        $ids = implode(',',array_column($datas,'id'));
        foreach($datas as $row){
            db('wallpaper')->where('title',$row['title'])->update(['noindex'=>0,'update_time'=>date('Y-m-d H:i:s')]);
            $cache_category_key = $cache_category_key.$row['category_id'] .'_'.$row['type'];
            cache($cache_category_key,date('Y-m-d'),['expire' => 0]);
            $type = $row['type'];
            $type_arr[] = $type;
            echo 'ids:'.$ids.' '.$row['title'].' '.$row['type']." date: ".cache($cache_category_key)."\n";
        }
        if(empty($type_arr)){
            return;
        }
        $type_arr = array_unique($type_arr);
        foreach ($type_arr as $type) {
            $cache_top_category_key = $cache_top_category_key.$type;
            cache($cache_top_category_key,date('Y-m-d'),['expire' => 0]);
        }
    }


    function sitemap() {
        $where = ['status' => 1,'noindex' => 0];
        $datas =  db('wallpaper')->where($where)->order('update_time asc')->limit(50000)->select();
        
        $pages = [];
        foreach($datas as $row){
            $type_name = $this->_type_names[$row['type']];
            $category_name = $this->_category_ids[$row['category_id']]['name'];
            $uri = '/category/'.rawurlencode($type_name).'/'.rawurlencode($category_name).'/'.rawurlencode($row['title']);
            $page['uri'] = "https://wallecho.com" .$uri;
            $page['date'] = date('Y-m-d',strtotime($row['update_time']));
            array_unshift($pages,$page);
        }
        //var_dump($pages);exit;
        unset($datas);
        sitemap($pages); 
    }
}